#define _GNU_SOURCE
#include "include/Login.h"
#include <shadow.h>
#include <crypt.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>

// 密码验证，使用shadow文件和crypt进行用户密码校验（二期功能）
int verify_password(const char *user, const char *pass) {
    struct spwd *sp = getspnam(user);
    if (!sp) return 0;
    char *encrypted = crypt(pass, sp->sp_pwdp);
    return encrypted && strcmp(encrypted, sp->sp_pwdp) == 0;
}

// 生成随机salt字符串
void generate_salt(char *salt, size_t len) {
    static const char salt_chars[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789./";
    srand((unsigned int)time(NULL) ^ getpid());
    for (size_t i = 0; i < len - 1; ++i) {
        salt[i] = salt_chars[rand() % (sizeof(salt_chars) - 1)];
    }
    salt[len - 1] = '\0';
}

// 获取shadow文件中的用户信息
struct spwd *get_user_info(const char *user) {
    return getspnam(user);
} 